﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Foothill.Service.UserAccount;
using Foothill.Service.SystemMessage;
using Foothill.IoC;
using Foothill.Service.Security;
using Foothill.Common;

namespace Foothill.Web
{
    public partial class ChangeTempPassword : System.Web.UI.Page
    {
        private IUserAccountService _userAccountService;
        private ISystemMessageService _systemMessageService;
        private ISecurityService _securityService;

        protected void Page_Load(object sender, EventArgs e)
        {
            _securityService = IocContainer.GetInstance<ISecurityService>();
            if (!_securityService.IsCurrentUserAuthenticated())
            {
                Response.Redirect("~/Login/Login.aspx");
            }
        }

        protected void btnSubmitClick(object sender, EventArgs e)
        {
            _userAccountService = IocContainer.GetInstance<IUserAccountService>();
            _systemMessageService = IocContainer.GetInstance<ISystemMessageService>();
            string errorMessage = string.Empty;

            if (_userAccountService.ChangePasswordAfterLoginUsingTempPassword(_securityService.GetLoginUserDetails(), txtNewPassword.Text.Trim(),
                txtConfirmPassword.Text.Trim(), out errorMessage))
            {
                DisplayChangePasswordMessage(_systemMessageService.GetSystemMessage(SystemMessageCategory.Security, SecurityMessageCode.ChangePassword_Succeed), "change-succeed");
            }
            else
            {
                DisplayChangePasswordMessage(errorMessage, "change-failed");
            }
        }

        private void DisplayChangePasswordMessage(string message, string cssClass)
        {
            lblChangePasswordMessage.Visible = true;
            lblChangePasswordMessage.Text = message;
            lblChangePasswordMessage.CssClass = cssClass;
        }
    }
}